<?php

/**
 * XHTML_Input, xhtml_lib
 * 
 * This file contains the source code for the XHTML_Input class.
 * @author Fernando Jimenez Mocobono <fjmcool@gmail.com>
 * @version 0.1
 * @package xhtml_lib
 */

include_once "XHTML_All_Attributes_Element.php";
include_once "XHTML__Inline_Forms.php";

/**
 * The XHTML_Input class is a handler for the input xhtml-strict tag as a part
 * of a xhtml-strict php-handler implementation.
 */
class XHTML_Input extends XHTML_All_Attributes_Element implements XHTML__Inline_Forms {
    
    // xhtml attributes
    
    /**
     * Type of the input element. The default value is "text". This is not a 
     * required attribute, but you should include it. If omitted, IE 5.5 will 
     * still display a text field, but Netscape 4.7 will not.
     * Valid values: text, password, checkbox, radio, submit, reset, file, 
     * hidden, image, button.
     * 
     * @var string
     */
    protected $type;
    
    /**
     * Unique name for the input element. This attribute is required with 
     * type="button", type="checkbox", type="file", type="hidden", 
     * type="image", type="password", type="text", and type="radio".
     *
     * @var string
     */
    protected $name;
    
    /**
     * For buttons, reset buttons and submit buttons: Text on the button.
     * For image buttons: Symbolic result of the field passed to a script.
     * For checkboxes and radio buttons: Result of the input element when 
     * clicked. The result is sent to the form's action URL.
     * For hidden, password, and text fields: Defines the default value of the 
     * element.
     * Notes: Cannot be used with type="file". This attribute is required with 
     * type="checkbox" and type="radio".
     *
     * @var string
     */
    protected $value;
    
    /**
     * Indicates that the input element should be checked when it first loads. 
     * Used with type="checkbox" and type="radio".
     *
     * @var boolean
     */
    protected $checked;
    
    /**
     * Disables the input element when it first loads so that the user can not 
     * write text in it, or select it. Cannot be used with type="hidden".
     * 
     * @var boolean
     */
    protected $disabled;
    
    /**
     * Indicates that the value of this field cannot be modified. Only used 
     * with type="text".
     *
     * @var boolean
     */
    protected $readOnly;
    
    /**
     * Size of the input element. Cannot be used with 
     * type="hidden".
     *
     * @var integer
     */
    protected $size;
    
    /**
     * Maximum number of characters allowed in a text field. Only used with 
     * type="text".
     *
     * @var integer
     */
    protected $maxLength;
    
    /**
     * URI of the image to display. Only used with type="image".
     *
     * @var string
     */
    protected $src;
    
    /**
     * Alternate text for the image. Only used with type="image".
     *
     * @var string
     */
    protected $alt;
    
    /**
     * URI that defines the image as a client-side image map.
     *
     * @var string
     */
    protected $useMap;
    
    /**
     * Script for the onselect event.
     *
     * @var string
     */
    protected $onSelectScript;
    
    /**
     * Script for the onchange event.
     *
     * @var string
     */
    protected $onChangeScript;
    
    /**
     * A comma-separated list of MIME types that indicates the MIME type of 
     * the file transfer. Only used with type="file".
     *
     * @var string
     */
    protected $accept;
    
    // Methods
    
    /**
     * Constructs a XHTML_Input object.
     */
    function __construct()
    {
    }
    
    /**
     * Sets the type of the input element. The default value is "text". This 
     * is not a required attribute, but you should include it. If omitted, 
     * IE 5.5 will still display a text field, but Netscape 4.7 will not.
     * Valid values: text, password, checkbox, radio, submit, reset, file, 
     * hidden, image, button.
     * 
     * @param string $type
     */
    function setType($type)
    {
        $this->type = $type;
    }
    
    /**
     * Gets the type of the input element. The default value is "text".
     * Valid values: text, password, checkbox, radio, submit, reset, file, 
     * hidden, image, button.
     *
     * @return string
     */
    function getType()
    {
        return $this->type;
    }
    
    /**
     * Sets the unique name for the input element. This attribute is required 
     * with type="button", type="checkbox", type="file", type="hidden", 
     * type="image", type="password", type="text", and type="radio".
     *
     * @param string $name
     */
    function setName($name)
    {
        $this->name = $name;
    }
    
    /**
     * Gets the unique name for the input element.
     *
     * @return string
     */
    function getName()
    {
        return $this->name;
    }
    
    /**
     * For buttons, reset buttons and submit buttons: Sets the Text on the 
     * button.
     * For image buttons: Sets the symbolic result of the field passed to a 
     * script.
     * For checkboxes and radio buttons: Sets the result of the input element 
     * when clicked. The result is sent to the form's action URL.
     * For hidden, password, and text fields: Sets the default value of the 
     * element.
     * Notes: Cannot be used with type="file". This attribute is required with 
     * type="checkbox" and type="radio".
     * 
     * @param string $value
     */
    function setValue($value)
    {
        $this->value = $value;
    }
    
    /**
     * For buttons, reset buttons and submit buttons: Gets the Text on the 
     * button.
     * For image buttons: Gets the symbolic result of the field passed to a 
     * script.
     * For checkboxes and radio buttons: Gets the result of the input element 
     * when clicked. The result is sent to the form's action URL.
     * For hidden, password, and text fields: Gets the default value of the 
     * element.
     * Notes: Not used with type="file".
     *
     * @return string
     */
    function getValue()
    {
        return $this->value;
    }
    
    /**
     * Indicates if the input element should be checked when it first loads. 
     * Used with type="checkbox" and type="radio".
     * 
     * @param boolean $checked
     */
    function setChecked($checked)
    {
        $this->checked = $checked;
    }
    
    /**
     * Checks if the input element should be checked when it first loads.
     * 
     * @return boolean
     */
    function isChecked()
    {
        return $this->checked;
    }
    
    /**
     * Indicates if the the input element should be disabled when it first 
     * loads so that the user can not write text in it, or select it. Cannot 
     * be used with type="hidden".
     * 
     * @param boolean $disabled
     */
    function setDisabled($disabled)
    {
        $this->disabled = $disabled;
    }
    
    /**
     * Checks if the the input element should be disabled when it first 
     * loads so that the user can not write text in it, or select it.
     *
     * @return boolean
     */
    function isDisabled()
    {
        return $this->disabled;
    }
    
    /**
     * Indicates if the value of this field cannot be modified. Only used 
     * with type="text".
     *
     * @param boolean $readOnly
     */
    function setReadOnly($readOnly)
    {
        $this->readOnly = $readOnly;
    }
    
    /**
     * Checks if the value of this field cannot be modified.
     * 
     * @return boolean
     */
    function isReadOnly()
    {
        return $this->readOnly;
    }
    
    /**
     * Sets the size of the input element. Cannot be used with type="hidden".
     * 
     * @param integer $size
     */
    function setSize($size)
    {
        $this->size = $size;
    }
    
    /**
     * Gets the size of the input element.
     * 
     * @return integer
     */
    function getSize()
    {
        return $this->size;
    }
    
    /**
     * Sets the maximum number of characters allowed in a text field. Only 
     * used with type="text".
     *
     * @param integer $maxLength
     */
    function setMaxLength($maxLength)
    {
        $this->maxLength = $maxLength;
    }
    
    /**
     * Gets the maximum number of characters allowed in a text field.
     * 
     * @return integer
     */
    function getMaxLength()
    {
        return $this->maxLength;
    }
    
    /**
     * Sets the URI of the image to display. Only used with type="image".
     *
     * @param string $src
     */
    function setSrc($src)
    {
        $this->src = $src;
    }
    
    /**
     * Gets the URI of the image to display.
     *
     * @return string
     */
    function getSrc()
    {
        return $this->src;
    }
    
    /**
     * Sets the alternate text for the image. Only used with type="image".
     *
     * @param string $alt
     */
    function setAlt($alt)
    {
        $this->alt = $alt;
    }
    
    /**
     * Gets the alternate text for the image.
     *
     * @return string
     */
    function getAlt()
    {
        return $this->alt;
    }
    
    /**
     * Sets the URI that defines the image as a client-side image map.
     *
     * @param string $useMap
     */
    function setUseMap($useMap)
    {
        $this->useMap = $useMap;
    }
    
    /**
     * Gets the URI that defines the image as a client-side image map.
     * 
     * @return string
     */
    function getUseMap()
    {
        return $this->useMap;
    }
    
    /**
     * Sets the script for the onselect event.
     * 
     * @param string $script
     */
    function setOnSelect($script)
    {
        $this->onSelectScript = $script;
    }
    
    /**
     * Gets the script for the onselect event.
     * 
     * @return string
     */
    function getOnSelect()
    {
        return $this->onSelectScript;
    }
    
    /**
     * Sets the script for the onchange event.
     * 
     * @param string $script
     */
    function setOnChange($script)
    {
        $this->onChangeScript = $script;
    }
    
    /**
     * Gets the script for the onchange event.
     * 
     * @return string
     */
    function getOnChange()
    {
        return $this->onChangeScript;
    }
    
    /**
     * Sets a comma-separated list of MIME types that indicates the MIME type of 
     * the file transfer. Only used with type="file".
     * 
     * @param string $accept
     */
    function setAccept($accept)
    {
        $this->accept = $accept;
    }
    
    /**
     * Gets a comma-separated list of MIME types that indicates the MIME type of 
     * the file transfer. Only used with type="file".
     * 
     * @return string
     */
    function getAccept()
    {
        return $this->accept;
    }


    // Overriding the toString method for a proper implementation.
    
    /**
    * Returns the string representation for echoing the class atributes.
    *
    * @return string
    */
    function toString()
    {
        $auxString = "<input".parent::toString();
        
        /*
         * Additions of the object own attributes to the return string.
         */
        
        if (isset($this->type)) {
            $auxString = $auxString."type=\"".$this->type."\" ";
        }
        
        if (isset($this->name)) {
            $auxString = $auxString."name=\"".$this->name."\" ";
        }
        
        if (isset($this->value)) {
            $auxString = $auxString."value=\"".$this->value."\" ";
        }
        
        if ($this->checked) {
            $auxString = $auxString."checked=\"checked\" ";
        }
        
        if ($this->disabled) {
            $auxString = $auxString."disabled=\"disabled\" ";
        }
        
        if ($this->readOnly) {
            $auxString = $auxString."readonly=\"readonly\" ";
        }
        
        if (isset($this->size)) {
            $auxString = $auxString."size=\"".$this->size."\" ";
        }
        
        if (isset($this->maxLength)) {
            $auxString = $auxString."maxlength=\"".$this->maxLength."\" ";
        }
        
        if (isset($this->src)) {
            $auxString = $auxString."src=\"".$this->src."\" ";
        }
        
        if (isset($this->alt)) {
            $auxString = $auxString."alt=\"".$this->alt."\" ";
        }
        
        if (isset($this->useMap)) {
            $auxString = $auxString."usemap=\"".$this->useMap."\" ";
        }
        
        if (isset($this->onSelectScript)) {
            $auxString = $auxString."onselect=\"".$this->onSelectScript."\" ";
        }
        
        if (isset($this->onChangeScript)) {
            $auxString = $auxString."onchange=\"".$this->onChangeScript."\" ";
        }
        
        if (isset($this->accept)) {
            $auxString = $auxString."accept=\"".$this->accept."\" ";
        }
        
        $auxString = $auxString."/>\n";
        
        return $auxString;
    }
    
}

?>